package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.RoleDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.RoleService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName RoleController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 15:03:09
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/role")
public class RoleController {

    private static final Logger LOG = LoggerFactory.getLogger(RoleController.class);
    public static final String BUSINESS_NAME = "角色";

    @Resource
    private RoleService roleService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        roleService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody RoleDto roleDto) {
        // 保存校验
        ValidatorUtil.require(roleDto.getId(), "Id");
        ValidatorUtil.length(roleDto.getId(), "Id", 1, 50);
        ValidatorUtil.require(roleDto.getName(), "角色名称");
        ValidatorUtil.length(roleDto.getName(), "角色名称", 1, 255);
        ValidatorUtil.require(roleDto.getStatus(), "当前状态");
        ValidatorUtil.require(roleDto.getCreatetime(), "创建时间");
        ValidatorUtil.length(roleDto.getCreateid(), "创建人ID", 1, 50);
        ValidatorUtil.length(roleDto.getTypename(), "分类名称", 1, 20);
        ValidatorUtil.length(roleDto.getTypeid(), "分类ID", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        roleService.save(roleDto);
        responseDto.setContent(roleDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();

        roleService.delete(id);
        return responseDto;
    }
}
